package cn.b.sky.admin.controller;

import cn.b.sky.admin.constants.SystemConstant;
import cn.b.sky.admin.model.*;
import cn.b.sky.admin.model.condition.ECodeTypeCondition;
import cn.b.sky.admin.model.condition.LanguageCondition;
import cn.b.sky.admin.service.AdminECodeService;
import cn.b.sky.admin.service.LanguagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <strong>Title : AdminECodeController.java<br>
 * </strong> <strong>Package : cn.b.sky.admin.controller<br>
 * </strong> <strong>Description : </strong>枚举信息控制器<br>
 * <strong>Create on : 2016-6-8 上午11:24:07<br>
 * </strong>
 * <p>
 * 
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/management/adminECode/*")
public class AdminECodeController extends BaseController {

  @Autowired
  private AdminECodeService adminECodeService;

  @Autowired
  private LanguagesService LanguagesService;

  /**
   * 
   * <strong>Title : listECodeTypesPageByCondition<br>
   * </strong> <strong>Description : </strong>根据枚举类型查询条件查询分页信息<br>
   * <strong>Create on : 2016-6-13 上午10:29:26<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面Tab的ID
   * @param condition
   *            查询条件信息
   * @param model
   *            数据模型
   * @return String 页面地址
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "listECodeTypesPageByCondition")
  public String listECodeTypesPageByCondition(String navTabId,
                                              @ModelAttribute("condition") ECodeTypeCondition condition, Model model) {
    model.addAttribute(NAVTABID, navTabId);

    model.addAttribute("page", this.adminECodeService.selectECodeTypesPageByCondition(condition));

    return "admin/ecode/eCodeType_list";
  }

  /**
   * 
   * <strong>Title : viewECodeType<br>
   * </strong> <strong>Description : </strong>根据枚举类型编码查询相关对应信息<br>
   * <strong>Create on : 2016-6-13 上午10:30:26<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面Tab的Id
   * @param eCodeType
   *            枚举类型编码
   * @param model
   *            数据模型
   * @return String 页面地址
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "viewECodeType")
  public String viewECodeType(String navTabId, String eCodeType, Model model) {

    model.addAttribute(NAVTABID, navTabId);

    model.addAttribute("eCodeType", this.adminECodeService.getECodeTypeAndAssociatedDataByECodeType(eCodeType));

    return "admin/ecode/eCodeType_view";
  }

  /**
   * 
   * <strong>Title : toAddECodeType<br>
   * </strong> <strong>Description : </strong>枚举类型编码信息保存前置操作<br>
   * <strong>Create on : 2016-6-13 上午10:31:25<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面Tab的ID
   * @param eCodeType
   *            枚举类型编码信息
   * @param model
   *            数据模型
   * @return String 页面地址
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toAddECodeType")
  public String toAddECodeType(String navTabId, String eCodeType, Model model) {

    model.addAttribute(NAVTABID, navTabId);

    return "admin/ecode/eCodeType_edit";
  }

  /**
   * 
   * <strong>Title : toEditECodeType<br>
   * </strong> <strong>Description : </strong>枚举类型编码信息修改前置操作<br>
   * <strong>Create on : 2016-6-13 上午10:32:04<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面Tab的ID
   * @param eCodeType
   *            枚举类型编码信息
   * @param model
   *            数据模型
   * @return String 页面地址
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toEditECodeType")
  public String toEditECodeType(String navTabId, String eCodeType, Model model) {

    model.addAttribute(NAVTABID, navTabId);

    model.addAttribute("eCodeType", this.adminECodeService.getECodeTypeAndAssociatedDataByECodeType(eCodeType));

    return "admin/ecode/eCodeType_edit";
  }

  /**
   * 
   * <strong>Title : saveECodeType<br>
   * </strong> <strong>Description : </strong>保存枚举类型信息及枚举码信息<br>
   * <strong>Create on : 2016-6-13 上午10:32:35<br>
   * </strong>
   * <p>
   ** 
   * @param navTabId
   *            页面Tab的ID
   * @param eCodeType
   *            枚举类型编码信息
   * @param model
   *            数据模型
   * @return DWZResult 操作结果信息
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "saveECodeType")
  @ResponseBody
  public DWZResult saveECodeType(String navTabId, ECodeType eCodeType, Model model) {
    if (null == eCodeType) {
      return ajaxError(SystemConstant.PARAMETERNULL);
    }
    try {
      this.adminECodeService.insertOrUpdateECodeTypeAndECode(eCodeType);
      return dialogAjaxDone(SystemConstant.DWZ_CODE_OK_MESSAGE, navTabId, null, null, null);
    } catch (Exception e) {
      return dialogAjaxDone(SystemConstant.DWZ_CODE_ERROR_MESSAGE, navTabId, null, null, null);
    }

  }

  /**
   * 
   * <strong>Title : deleteECodeTypeAndAssociatedDataByECodeType<br>
   * </strong> <strong>Description : </strong>删除枚举类型及枚举码相关信息<br>
   * <strong>Create on : 2016-6-13 上午10:33:06<br>
   * </strong>
   * <p>
   * 
   * @param model
   *            数据模型
   * @param navTabId
   *            页面Tab的Id
   * @param request
   *            http请求对象
   * @return DWZResult 操作结果信息
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("deleteECodeTypeAndAssociatedDataByECodeType")
  @ResponseBody
  public DWZResult deleteECodeTypeAndAssociatedDataByECodeType(Model model, String navTabId,
                                                               HttpServletRequest request) {
    String eCodeTypes = request.getParameter("ids");
    if (StringUtils.isEmpty(eCodeTypes)) {
      return ajaxError(SystemConstant.PARAMETERNULL);
    }
    String[] eCodeTypeArray = eCodeTypes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(eCodeTypeArray.length);
    boolean hasFail = false;
    BatchJobReport report = null;
    for (String eCodeType : eCodeTypeArray) {
      report = this.adminECodeService.deleteECodeTypeAndAssociatedDataByECodeType(eCodeType);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_OK_MESSAGE, null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  /**
   * 
   * <strong>Title : beforeTranslate<br>
   * </strong> <strong>Description : </strong>枚举码翻译前置操作<br>
   * <strong>Create on : 2016-6-13 上午10:36:05<br>
   * </strong>
   * <p>
   * 
   * @param model
   *            数据模型
   * @return String 页面地址
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("beforeTranslate")
  public String beforeTranslate(Model model) {
    LanguageCondition condition = new LanguageCondition();
    model.addAttribute("languages", this.LanguagesService.selectLanguageByCondition(condition));
    return "admin/ecode/eCodeType_translate";
  }

  /**
   * 
   * <strong>Title : toTranslate<br>
   * </strong> <strong>Description :
   * </strong>根据枚举类型信息、源语言及目标语言查询相应信息并返回相应数据结果集<br>
   * <strong>Create on : 2016-6-13 上午10:36:24<br>
   * </strong>
   * <p>
   * 
   * @param eCodeType
   *            枚举类型编码
   * @param sourceLanguageCode
   *            源语言信息
   * @param toLanguageCode
   *            目标语言信息
   * @return DWZResult 操作结果
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("toTranslate")
  @ResponseBody
  public DWZResult toTranslate(String eCodeType, String sourceLanguageCode, String toLanguageCode) {
    List<ECode> eCodeList = this.adminECodeService.selectEcodeListForTranslate(eCodeType.toUpperCase(),
        sourceLanguageCode, toLanguageCode);
    return ajaxDone(null,eCodeList);
  }

  /**
   * 
   * <strong>Title : saveTranslate<br>
   * </strong> <strong>Description : </strong>保存枚举码翻译信息<br>
   * <strong>Create on : 2016-6-13 上午10:37:23<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面TabId
   * @param languageCode
   *            翻译所对应的语言编码
   * @param eCodeType
   *            枚举类型信息
   * @return DWZResult 操作结果
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("saveTranslate")
  @ResponseBody
  public DWZResult saveTranslate(String navTabId, String languageCode, ECodeType eCodeType) {
    try {
      for (ECodeTl eCodeTl : eCodeType.geteCodeTlList()) {
        eCodeTl.setLanguageCode(languageCode);
      }
      this.adminECodeService.insertOrUpdateECodeTlBatch(eCodeType.geteCodeTlList());
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_OK_MESSAGE, null);
    } catch (Exception e) {
      // 返回出错消息
      return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_ERROR_MESSAGE, null);
    }

  }

  /**
   * 
   * <strong>Title : validateECodeType<br>
   * </strong> <strong>Description : </strong>验证枚举类型信息是否存在<br>
   * <strong>Create on : 2016-6-13 上午10:43:37<br>
   * </strong>
   * <p>
   * 
   * @param eCodeType
   *            枚举类型编码
   * @param model
   *            数据模型
   * @return boolean 是否存在
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("validateECodeType")
  @ResponseBody
  public boolean validateECodeType(String eCodeType, Model model) {
    if (StringUtils.isNotBlank(eCodeType) && null != this.adminECodeService.getECodeTypeByPK(eCodeType.toUpperCase())) {
      return false;
    }

    return true;
  }

}
